package com.tansun.easycare.modules.rulenamalist.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.core.utils.ResponseTable;
import com.tansun.easycare.rule.common.model.ResponseVO;
import com.tansun.easycare.rule.datamodel.entity.RuleDatamodel;
import com.tansun.easycare.rule.front.entity.RuleDatasource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.tansun.easycare.core.utils.StringUtils;
import com.tansun.easycare.core.web.BaseController;
import com.tansun.easycare.modules.rulenamalist.entity.RuleNamelist;
import com.tansun.easycare.modules.rulenamalist.service.RuleNamelistService;

/**
 * 名单配置Controller
 * @author lph
 * @version 2019-06-18
 */
@RestController
@RequestMapping(value = "/namalist/ruleNamelist")
public class RuleNamelistController extends BaseController {

	@Autowired
	private RuleNamelistService ruleNamelistService;
	
	//@ModelAttribute
	@RequestMapping("getById")
	public ResponseVO get(@RequestBody RuleNamelist ruleNamelist) {
		ResponseVO ret = new ResponseVO();
		try {
			RuleNamelist data = this.ruleNamelistService.get(ruleNamelist.getId());
			ret.setData(data);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			ret.setStatus(ResponseVO.ERROR);
			ret.setDescription(e.getMessage());
		}
		return ret;
	}

	/**
	 * 名单配置首页
	 * @param ruleNamelist
	 * @return
	 */
	@RequestMapping(value = {"list", "","index"})
	public ResponseTable<RuleNamelist> list(@RequestBody RuleNamelist ruleNamelist) {
		PageInfo<RuleNamelist> pageInfo = new PageInfo<>(this.ruleNamelistService.findList(ruleNamelist));
		ResponseTable<RuleNamelist>  resVo  = new  ResponseTable<RuleNamelist>(pageInfo);
		return resVo;
	}

	@RequestMapping(value = "form")
	public String form(RuleNamelist ruleNamelist, Model model) {
		model.addAttribute("ruleNamelist", ruleNamelist);
		return "modules/namalist/ruleNamelistForm";
	}

	@RequestMapping(value = "deleteNameList")
	public ResponseVO deleteNameList(@RequestBody RuleNamelist ruleNamelist) {
		ResponseVO responseVO = new ResponseVO();
		responseVO.setStatus(200);
		try {
			this.ruleNamelistService.delete(ruleNamelist);
		} catch (Exception e) {
			responseVO.setStatus(500);
			responseVO.setDescription(e.getMessage());
			logger.error(e.getMessage(),e);
		}
		return responseVO;
	}

	@RequestMapping(value = "saveOrUpdate")
	public ResponseVO saveOrUpdate(@RequestBody RuleNamelist ruleNamelist) {
		ResponseVO ret = new ResponseVO();
		try {
			this.ruleNamelistService.save(ruleNamelist);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			ret.setStatus(ResponseVO.ERROR);
			ret.setDescription(e.getMessage());
		}
		return ret;

	}



}